package com.whut.whw;

import android.app.Application;
import android.content.SharedPreferences;
import android.net.Uri;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.whut.whw.bean.Users;
import com.whut.whw.login.LoginActivity;
import com.whut.whw.util.GsonUtils;
import com.whut.whw.util.Utils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.UserInfo;

/**
 * <pre>
 *     author : 杨丽金
 *     time   : 2017/06/02
 *     desc   : 用于保存全局变量
 *     version: 1.0
 * </pre>
 */
public class App extends Application {
    private List<Users> list;

    @Override
    public void onCreate() {
        super.onCreate();
        // 初始化Utils中的应用程序的上下文
        Utils.init(getApplicationContext());
        // 初始化ImageLoder的配置
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);
        ImageLoader.getInstance().init(configuration);
        //tc 融云初始化
        RongIM.init(this);
        SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
        String token = sp.getString("token", "");
        if (!"".equals(token)) {
            connectRong(token);
        }
    }

    /**
     * 连接融云
     *
     * @param token
     */
    private void connectRong(String token) {
        /**
         * 测试
         */
        list = new ArrayList<>();
        try {
            JSONArray array = new JSONArray();
            JSONObject obj = new JSONObject();
            obj.put("user_id", "123456");
            obj.put("user_name", "忘尘无憾");
            obj.put("user_img", "http://upload.jianshu.io/users/upload_avatars/3358298/167277b0-84f6-4b2c-a740-d117ab4687b6.jpg");
            array.put(obj);
            JSONObject obj1 = new JSONObject();
            obj1.put("user_id", "111");
            obj1.put("user_name", "若忘尘");
            obj1.put("user_img", "https://ss3.baidu.com/7LsWdDW5_xN3otqbppnN2DJv/pass/s%3D110%3Bq%3D90%3Bg%3D0/sign=e233f4b84a4a20a4351e38c6a069e91f/08f790529822720eadd64e507ecb0a46f21fab04.jpg");
            array.put(obj1);
            list = GsonUtils.jsonToList(array.toString(), Users.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

        RongIM.connect(token, new RongIMClient.ConnectCallback() {
            @Override
            public void onTokenIncorrect() {
                Toast.makeText(App.this, "onTokenIncorrect", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSuccess(String s) {

                RongIM.setUserInfoProvider(new RongIM.UserInfoProvider() {
                    @Override
                    public UserInfo getUserInfo(String s) {
                        for (Users user : list) {
                            if (user.getUser_id().equals(s)) {
                                return new UserInfo(user.getUser_id(), user.getUser_name(), Uri.parse(user.getUser_img()));
                            }
                        }
                        return null;
                    }
                }, true);
                Toast.makeText(App.this, "RongYun Connect Success", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {
                Toast.makeText(App.this, "RongYun Connect Fail", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
